package activitiOne;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Test;

import java.io.*;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 第一个完整的流程
 * 采购申请流程
 */
public class FirstActiviti {
    /**
     * 获得流程引擎
     */
    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

    /**
     * 部署
     */
    @Test
    public void distory(){
        Deployment deployment = processEngine.getRepositoryService()
                .createDeployment()
                .name("采购申请流程")
                .addClasspathResource("diagrams/payBill.bpmn")
                .addClasspathResource("diagrams/payBill.png")
                .deploy();
        System.out.println("部署ID" + deployment.getId());
        System.out.println("部署时间" + deployment.getDeploymentTime());
    }

    /**
     * 启动流程 配置任务节点变量
     * */
    @Test
    public void startProcess(){
        String processDefinitionKey = "payBill";
        Map<String,Object> variables = new HashMap<>();
        variables.put("assignee","张三");
        variables.put("jingli","李四");
        variables.put("caiwu","王五");
        variables.put("laoban","Boss");
        variables.put("money",5000);
        ProcessInstance pi = processEngine.getRuntimeService()
                .startProcessInstanceByKey(processDefinitionKey,variables);
        System.out.println("流程实例ID："+pi.getId());
        System.out.println("流程定义的ID："+pi.getProcessDefinitionId());
    }

    /**
     * 设置流程变量
     */
    @Test
    public void setVariables(){
        String taskId = "2509";//任务ID
        TaskService taskService = processEngine.getTaskService();
        taskService.setVariableLocal(taskId,"任务开始时间",new Date());//该变量只在任务中有效
        taskService.setVariable(taskId,"全局变量","我是全局变量");
        System.out.println("设置成功");
    }

    /**
     * 获取流程变量
     */
    @Test
    public void getVariables(){
        String taskId = "7504";//任务ID
        TaskService taskService = processEngine.getTaskService();
        Date date = (Date) taskService.getVariableLocal(taskId,"任务开始时间");//该变量只在任务中有效
        String str =(String) taskService.getVariable(taskId,"全局变量");
        String assignee = (String) taskService.getVariable(taskId,"assignee");
//        System.out.println("任务" + taskId + "的开始时间是==》" +date.toLocaleString());
        System.out.println("申请人==》" + assignee);
        System.out.println("测试全局变量==》" + str);
    }

    /**
     * 查询任务
     */
    @Test
    public void getTaskByName(){
        String assignee = "李四";
        List<Task> list = processEngine.getTaskService()
                .createTaskQuery()
                .taskAssignee(assignee)
                .list();
        if(list != null && list.size() > 0){
            for(Task task:list){
                System.out.println("任务的办理人："+task.getAssignee());
                System.out.println("任务ID："+task.getId());
                System.out.println("任务名称："+task.getName());
                System.out.println("任务的创建时间："+task.getCreateTime());
                System.out.println("流程实例ID："+task.getProcessInstanceId());
                System.out.println("#######################################");
            }
        }
    }

    /**
     * 完成任务
     */
    @Test
    public void completeTask(){
        String templete = "1";
        String taskId = "17504";
        Map<String , Object> map = new HashMap<>();
        map.put("templete",templete);
        processEngine.getTaskService().complete(taskId,map);
        System.out.println("完成任务："+taskId);
    }

    /**
     * 获取流程定义
     */
    @Test
    public void queryProcessDefination(){
        String proDefiKey = "payBill";
        List<ProcessDefinition> list = processEngine.getRepositoryService()
                .createProcessDefinitionQuery()
//                .processDefinitionId(proDefiId) //流程定义id
                .processDefinitionKey(proDefiKey)//流程定义key
//                .processDefinitionName(name)//流程定义名称
//                .processDefinitionVersion(version)//流程定义的版本
//                .latestVersion()//最新版本
                //排序
                .orderByProcessDefinitionVersion().desc()//按照版本降序排班
                .list();

        if(null != list && list.size()>0){
            for(ProcessDefinition temp : list){
                System.out.println("流程实例ID：" + temp.getDeploymentId());
                System.out.println("流程定义ID：" + temp.getId());
                System.out.println("流程定义名称：" + temp.getName());
                System.out.println("流程定义版本：" + temp.getVersion());
                System.out.println("流程定义Key：" + temp.getKey());
            }
        }
    }

    /**
     * 删除流程实例
     */
    @Test
    public void delProcessDefination(){
        String deployment = "12501";
        processEngine.getRepositoryService().deleteDeployment(deployment);
        System.out.println("删除流程" + deployment + "成功");
    }

    /**
     * 读取资源
     */
    @Test
    public void getImage() throws IOException {
        String deploymentId = "12501";
        String imageName = "";
        //获取资源文件的名称 deployMentId
        List<String> resourceNames = processEngine.getRepositoryService()
                .getDeploymentResourceNames(deploymentId);
        if(null != resourceNames && resourceNames.size()>0){
            for (String temp : resourceNames){
                if(temp.indexOf(".png") > 0){
                    imageName = temp;
                }
            }
        }

        InputStream resourceAsStream = processEngine.getRepositoryService()
                .getResourceAsStream(deploymentId,imageName);
        byte[] buffer = new byte[resourceAsStream.available()];
        resourceAsStream.read(buffer);

        //把文件输入流写入到文件中
        File file = new File("E:/" + imageName);
        if (!file.exists()) {
            //创建文件夹
            file.getParentFile().mkdir();
            //创建文件
            file.createNewFile();
        }
        OutputStream outputStream = new FileOutputStream(file);
        outputStream.write(buffer);
        outputStream.close();
    }

}
